package com.springboot.jwt.exception;


import com.alibaba.fastjson.JSONObject;

public class ResultResponse {

    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private Object data;

    public ResultResponse() {

    }

    public ResultResponse(BaseErrorInfoInterface errorInfoInterface) {
        this.code = errorInfoInterface.getResultCode();
        this.message = errorInfoInterface.getResultMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 成功
     * @return
     */
    public static ResultResponse success() {
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static ResultResponse success(Object data)
    {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(ExceptionEnum.SUCCESS.getResultCode());
        resultResponse.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
        resultResponse.setData(data);
        return resultResponse;
    }

    /**
     * 失败
     * @param errorInfoInterface
     * @return
     */
    public static ResultResponse error(BaseErrorInfoInterface errorInfoInterface) {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(errorInfoInterface.getResultCode());
        resultResponse.setMessage(errorInfoInterface.getResultMsg());
        resultResponse.setData(null);
        return resultResponse;
    }

    public static ResultResponse error(String code, String message) {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(code);
        resultResponse.setMessage(message);
        resultResponse.setData(null);
        return resultResponse;
    }

    public static ResultResponse error(String message)
    {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode("-1");
        resultResponse.setMessage(message);
        resultResponse.setData(null);
        return resultResponse;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
